package com.qq.financeSystem.service.impl;

import com.qq.financeSystem.mapper.OffsetMapper;
import com.qq.financeSystem.mapper.SendSalaryMapper;
import com.qq.financeSystem.model.CarRoyalty;
import com.qq.financeSystem.model.EmpSalary;
import com.qq.financeSystem.model.ServiceMoney;
import com.qq.financeSystem.model.YearAndMonth;
import com.qq.financeSystem.service.SendSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SendSalaryServiceImpl implements SendSalaryService {

    @Autowired
    SendSalaryMapper sendSalaryMapper;
    @Autowired
    OffsetMapper offsetMapper;

    @Override
    public List<EmpSalary> queryAllSalary(String identificationCode) {
        List<EmpSalary> emp = sendSalaryMapper.selectAllEmpSalary(identificationCode);
        for (int i = 0; i < emp.size(); i++) {
            Double commission = 0.0;
            if ("1".equals(identificationCode)) {
                commission = sendSalaryMapper.selectCarSumById(emp.get(i).getEmployeeId());
            } else if ("3".equals(identificationCode)) {
                commission = sendSalaryMapper.selectServiceSumById(emp.get(i).getEmployeeId());
            }
            if (commission != null)
                emp.get(i).setCommission(commission);
            else
                emp.get(i).setCommission(0.0);
        }
        if ("2".equals(identificationCode)) {
            for (EmpSalary e : emp) {
                e.setCommission(0.0);
            }
        }
        for (int i = 0; i < emp.size(); i++) {
            Double offset = offsetMapper.selectSumMoneyById(emp.get(i).getEmployeeId());
            if (offset != null)
                emp.get(i).setOffsetMoney(offset);
            else
                emp.get(i).setOffsetMoney(0.0);
        }
        return emp;
    }

    @Override
    public List<EmpSalary> queryOneById(String employeeId) {
        return sendSalaryMapper.selectOneById(employeeId);
    }

    @Override
    public List<EmpSalary> selectAllEmpSalaryByKey(YearAndMonth key) {
        return sendSalaryMapper.selectAllEmpSalaryByKey(key);
    }

    @Override
    public int queryHasOneById(String employeeId) {
        EmpSalary empSalary = sendSalaryMapper.selectHasOneById(employeeId);
        if (empSalary != null)
            return 1;
        return 0;
    }

    @Override
    public EmpSalary queryOneInfoById(String employeeId, String ident) {
        EmpSalary empSalary = new EmpSalary();
        if ("2".equals(ident)) {
            empSalary = sendSalaryMapper.selectHomeOneInfo(employeeId);
            empSalary.setCommission(0.0);
        } else {
            if ("1".equals(ident)) {
                empSalary = sendSalaryMapper.selectCarOneInfo(employeeId);
            } else if ("3".equals(ident)) {
                empSalary = sendSalaryMapper.selectServiceOneInfo(employeeId);
            }
            Double offset = offsetMapper.selectSumMoneyById(employeeId);
            if (offset != null)
                empSalary.setOffsetMoney(offset);
            empSalary.setOffsetMoney(0.0);
        }
        Date date = new Date();
        Double sumMoney = empSalary.getOffsetMoney()+empSalary.getCommission()+empSalary.getSalary();
        empSalary.setTimes(date);
        empSalary.setSumMoney(sumMoney);
        return empSalary;
    }

    @Override
    public int insertEmpSalary(EmpSalary empSalary) {
        return sendSalaryMapper.insertEmpSalary(empSalary);
    }

    @Override
    public List<CarRoyalty> queryCarById(String employeeId) {
        return sendSalaryMapper.selectCarById(employeeId);
    }

    @Override
    public List<ServiceMoney> queryServiceById(String employeeId) {
        return sendSalaryMapper.selectServiceById(employeeId);
    }

    @Override
    public List<CarRoyalty> queryLastCarById( YearAndMonth key) {
        return sendSalaryMapper.selectLastCarById(key);
    }

    @Override
    public List<ServiceMoney> queryLastServiceById( YearAndMonth key) {
        return sendSalaryMapper.selectLastServiceById(key);
    }

}
